//build args: gcc -g ogl_tri.c -o ogl_tri -lglut

#include <GL/glut.h>
#include <unistd.h>

const int  width = 800;
const int  height = 600;


GLvoid InitGL(GLvoid);
GLvoid DrawGL(GLvoid);
GLvoid ResizeGL(GLsizei  width,GLsizei height);

int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE |GLUT_RGBA |GLUT_DEPTH);

  glutInitWindowSize(width,height);

  glutCreateWindow("Window");

  glutDisplayFunc(DrawGL);

  InitGL();

  glutMainLoop();

  return 0;
}


GLvoid ResizeGL(GLsizei width,GLsizei height)
{
  if(height == 0)
    {
      height = 1;
    }

  glViewport(0,0,width,height);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluPerspective(45.0f,(GLfloat)width /(GLfloat)height,0.1f,100.0f);

  glMatrixMode(GL_MODELVIEW);

  glLoadIdentity();

}

GLvoid InitGL(GLvoid)
{
  glShadeModel(GL_SMOOTH);//activates smooth shading

  glClearColor(0.0f,0.0f,0.0f,0.0f);//makes bg color black

  glClearDepth(1.0f);//sets depth to 1.0?

  glEnable(GL_DEPTH_TEST);//tests depth

  glDepthFunc(GL_LEQUAL);//depth testing functions

  glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST);//hints what perspective to use
}


GLvoid DrawGL(GLvoid)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears the buff rendering
  glLoadIdentity(); //rerenders?

  glColor3f(1.0f,1.0f,1.0f);

  glBegin(GL_TRIANGLES);//creates a tri

  glVertex3f( 0.0f, 1.0f, 0.0f);
  glVertex3f(-1.0f,-1.0f, 0.0f);
  glVertex3f( 1.0f,-1.0f, 0.0f);

  glEnd();//ends

  glFlush(); //clears all commmands

  glutSwapBuffers();//switches buffers

  glutPostRedisplay();//redraws
}
